package com.isky.visual.result;




import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName : ResultVo
 * @Description : 返回给页面的对象模型
 * @Author :wudl
 * @Date: 2020-04-12 14:46
 */

@Data
public class ResultVo<T> implements Serializable {
    private Integer code;
    private String msg;

    public ResultVo() {

    }
    public ResultVo(Integer code) {
        this.code = code;
    }

    private T data;

    public ResultVo(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResultVo(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public ResultVo(Integer code, String msg,T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public ResultVo(T data) {
        this.data = data;
    }

    private ResultVo(CodeMsg codeMsg) {
        if (codeMsg != null) {
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
        }
    }

    /**
     * 成功时候的调用
     */
    public static <T> ResultVo<T> success(T data) {
        Integer code = CodeMsg.SUCCESS.getCode();
        String  msg = CodeMsg.SUCCESS.getMsg();
        return new ResultVo<T>(code,msg,data);
    }

//    public static <T> ResultVo<T> success(String msg) {
//        return new ResultVo<T>(CodeMsg.SUCCESS.getCode(), msg);
//    }

    public static <T> ResultVo<T> success() {
        return new ResultVo<T>(CodeMsg.SUCCESS.getCode(), CodeMsg.SUCCESS.getMsg());
    }


    /**
     * 失败时候的调用
     */
    public static <T> ResultVo<T> error() {
        return new ResultVo<T>(CodeMsg.SERVER_ERROR);
    }
    public static <T> ResultVo<T> error(CodeMsg codeMsg) {
        return new ResultVo<T>(codeMsg);
    }

    public static <T> ResultVo<T> error(T data) {
        return new ResultVo<T>(data);
    }

    public static <T> ResultVo<T> error(int code, String msg) {
        return new ResultVo<T>(code, msg);
    }

}